<?php
namespace app\common\business;

class BusBase
{

    public function status($data)
    {
        if(!isset($data['id']) || !isset($data['status'])) {
            return false;
        }

        $res = $this->getById($data['id']);
        if (!$res) {
            throw new \think\Exception('不存在该记录');
        }

        if ($res['status'] == $data['status']) {
            throw new \think\Exception('两次状态修改相同');
        }

        $updateData = [
            'status' => intval($data['status']),
        ];

        try {
            $res = $this->model->updateById($data['id'], $updateData);
        } catch (\Exception $e) {
            return false;
        }
        return $res;
    }

    public function listorder($data)
    {
        if(!isset($data['id']) || !isset($data['listorder'])) {
            return false;
        }

        $res = $this->getById($data['id']);
        if (!$res) {
            throw new \think\Exception('不存在该记录');
        }

        $updateData = [
            'listorder' => intval($data['listorder']),
        ];

        try {
            $res = $this->model->updateById($data['id'], $updateData);
        } catch (\Exception $e) {
            return false;
        }
        return $res;
    }


    public function getById($id): array
    {
        $result =  $this->model->find($id);
        if (!$result) {
            return [];
        }
        return $result->toArray();
    }
}
